package com.senthink.gl.business.domain.service;

import com.senthink.gl.business.common.entity.SearchRequest;
import com.senthink.gl.business.common.exception.DataAccessException;
import com.senthink.gl.business.domain.entity.*;
import com.senthink.gl.business.domain.repository.PeopleEpiRepository;
import com.senthink.gl.business.domain.repository.PeopleLiveRepository;
import com.senthink.gl.business.domain.repository.PeopleRepository;
import com.senthink.gl.business.security.CurrentUser;
import com.senthink.gl.business.security.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;

/**
 * 灯具数据服务
 *
 * @author zooqi@senthink.com
 * @date 2017/11/27
 */
@Service
public class PeopleDataService {

    @Autowired
    private PeopleRepository repository;
    @Autowired
    private PeopleLiveRepository peopleLiveRepository;
    @Autowired
    private PeopleEpiRepository peopleEpiRepository;


    /**
     * 获取详情
     *
     * @param pid 唯一id
     * @return
     */
    public Optional<People> findByPid(String pid) {
        try {
            return repository.findTopByPid(pid);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    /**
     * 获取详情
     *
     * @param idcard
     * @return
     */
    public Optional<People> findTopByIdcard(String idcard) {
        try {
            return repository.findTopByIdcard(idcard);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    /**
     * 删除
     *
     * @param pid 唯一id
     * @return
     */
    public boolean deleteByPid(String pid) {
        try {
            if (repository.deleteByPid(pid) > 0) {
                return true;
            }
            return false;
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    /**
     * 删除
     *
     * @param people
     * @return
     */
    public void delete(People people) {
        try {
            repository.delete(people);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    /**
     * 通过sn计数
     *
     * @param idcard
     * @return
     */
    public long countByIdcard(String idcard) {
        try {
            return repository.countByIdcard(idcard);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    /**
     * 保存/更新
     *
     * @param people 需要保存/更新的实体
     * @return
     */
    public People save(People people) {
        try {
            return repository.save(people);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    public PeopleEpi saveEpi(PeopleEpi peopleEpi) {
        try {
            return peopleEpiRepository.save(peopleEpi);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }



    /**
     * 分页+筛选+搜索灯具列表
     *
     * @param searchRequest 搜索参数
     * @param pageRequest   分页参数
     * @return
     */
    public Page<People> findAll(SearchRequest searchRequest, PageRequest pageRequest) {
        Page<People> page;
        CurrentUser user = SecurityUtils.getCurrentUser();
       
        try {
            page = repository.findAll((root, query, cb) -> {
                List<Predicate> predicates = new LinkedList<>();

                if (searchRequest != null) {
                    predicates.add(searchRequest.generatePredicate(root, cb));
                }
                return cb.and(predicates.toArray(new Predicate[predicates.size()]));
            }, pageRequest);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
        return page;
    }

    /**
     * 查找
     *
     * @param people
     * @return
     */
    public List<PeopleLive> findAllLiveByPeople(People people) {
        try {
            return peopleLiveRepository.findAllByPeople(people);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    public Optional<PeopleEpi> findTopByPeople(People people) {
        try {
            return peopleEpiRepository.findTopByPeople(people);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    public Optional<PeopleLive> findTopByPlid(String plid) {
        try {
            return peopleLiveRepository.findTopByPlid(plid);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    public Optional<PeopleEpi> findTopByPeid(String peid) {
        try {
            return peopleEpiRepository.findTopByPeid(peid);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }


    public List<PeopleLive> saveItems(Iterable<PeopleLive> items) {
        try {
            return peopleLiveRepository.save(items);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    public void deleteLive(People people) {
        try {
            peopleLiveRepository.deleteAllByPeople(people);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }

    public void deleteEpi(People people) {
        try {
            peopleEpiRepository.deleteAllByPeople(people);
        } catch (Exception e) {
            throw new DataAccessException(e);
        }
    }



}
